using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

/// <summary>
/// Summary description for DataProvider
/// </summary>
    public class DataProvider : DataTable
    {
        /// <summary>
        /// Constructor Class
        /// </summary>
        public DataProvider()
        {            
        }
        /*
         * Declare member variable
         */
        private SqlDataAdapter mDataAdapter;
        private string mStringSQL;
        private string mTaName;
        private static SqlConnection mConn;

        /*
         *  Declare property
         */
        public static string StringDB = ConfigurationManager.ConnectionStrings["TACO_Website"].ConnectionString;
        public string StringSQL
        {
            get { return mStringSQL; }
            set { mStringSQL = value; }
        }
        public string TaName
        {

            get { return mTaName; }
            set { mTaName = value; }
        }
        public static SqlConnection Conn
        {

            get { return mConn; }
            set { mConn = value; }
        }
        public long RowsCount
        {
            /*
             * count rows in table
             */            
            get { return this.Rows.Count; }
        }

        public void GetData(string pTaName, string pStringSQL)
        {
            mTaName = pTaName;
            mStringSQL = pStringSQL;
            ReadTable();
        }

        private DataTable ReadTable()
        {
            if (mConn == null)
            {
                mConn = new SqlConnection();                
            }
            mConn.ConnectionString = StringDB;
            try
            {
                mDataAdapter = new SqlDataAdapter(mStringSQL, mConn);
                mDataAdapter.FillSchema(this, SchemaType.Mapped);
                mDataAdapter.Fill(this);

                SqlCommandBuilder CommandBD = new SqlCommandBuilder(mDataAdapter);
                return this;
            }
            catch (SqlException ex)
            {
                return null;
            }
        }
        
        public DataTable GetDataFromStore(string pStoreName,string[] ParamName,object[] ParamValue )
        {
            if (mConn == null)
            {
                mConn = new SqlConnection();                
            }
            mConn.ConnectionString = StringDB;
            try
            {
                SqlCommand cmd = new SqlCommand(pStoreName, mConn);
               if(ParamValue!=null)
                for (int i = 0; i < ParamName.Length; i++)
                    cmd.Parameters.AddWithValue(ParamName[i], ParamValue[i]);
                cmd.CommandType = CommandType.StoredProcedure;

                mDataAdapter = new SqlDataAdapter(cmd);
                mDataAdapter.Fill(this);

                SqlCommandBuilder CommandBD = new SqlCommandBuilder(mDataAdapter);
                CommandBD.Dispose();
                cmd.Dispose();
                return this;
            }
            catch (SqlException ex)            
            {
                return null;
            } 
        }        
    }

